home *** CD-ROM | disk | FTP | other *** search
/ Aminet 23 / Aminet 23 (1998)(GTI - Schatztruhe)[!][Feb 1998].iso / Aminet / util / boot / StackAttack.lha / StackAttack / Source / main.c next >
C/C++ Source or Header  |  1997-11-03  |  4KB  |  178 lines

  1. #include <OSIncludes.h>
  2.  
  3. #pragma header
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. extern "ASM" void New_CreateProc(void);
  9. extern "ASM" void New_CreateNewProc(void);
  10. extern "ASM" void New_SystemTagList(void);
  11. extern "ASM" void New_RunCommand(void);
  12.  
  13. char *VersionString = "$VER: StackAttack 1.2 ("__DATE2__")";
  14. char *AuthorString  = "$AUTH: Georg Steger";
  15.  
  16. char *TEMPLATE         = "MINSTACK=MIN/N/K,ADDSTACK=ADD/N/K";
  17.     
  18. enum {ARG_MINSTACK,ARG_ADDSTACK,NUM_ARGS};
  19.     
  20. #define LVO_CreateProc -138
  21. #define LVO_CreateNewProc -498
  22. #define LVO_SystemTagList -606
  23. #define LVO_RunCommand -504
  24.     
  25.  
  26. struct RDArgs *MyArgs;
  27. LONG    *Args[NUM_ARGS];
  28.  
  29. BOOL    DontCloseUtility;
  30.  
  31. extern "C" // C-Style variables in patch.asm
  32. {
  33.     struct UtilityBase *UtilityBase;
  34.  
  35.     struct Library *CDOSBase;
  36.  
  37.     APTR    Old_CreateProc,
  38.             Old_CreateNewProc,
  39.             Old_SystemTagList,
  40.             Old_RunCommand;
  41.  
  42.     ULONG MinStack,StackAdd;
  43. };
  44.  
  45. extern "ASM"
  46. {
  47.     void PATCHSTART(void);
  48.     void PATCHEND(void);
  49. };
  50.  
  51. void Cleanup(LONG rc)
  52. {
  53.     if (UtilityBase && (!DontCloseUtility)) CloseLibrary((struct Library *)UtilityBase);
  54.     exit(rc);
  55. }
  56.  
  57. void Init(void)
  58. {
  59.     /* DOSBase is already open and will be closed by the
  60.        compiler, so we use another name for the variable  */
  61.     CDOSBase=OpenLibrary("dos.library",36);
  62. }
  63.  
  64. void OpenLibs(void)
  65. {
  66.     if (!(UtilityBase=(struct UtilityBase *)OpenLibrary(UTILITYNAME,36)))
  67.     {
  68.         Cleanup(RETURN_WARN);
  69.     }
  70. }
  71.  
  72. void GetArguments(void)
  73. {
  74.     BOOL ok=FALSE;
  75.  
  76.     if ((MyArgs=ReadArgs(TEMPLATE,(LONG *)Args,0)))
  77.     {
  78.         if (Args[ARG_MINSTACK])
  79.         {
  80.             MinStack=(*Args[ARG_MINSTACK]+3)&0xFFFFFFFC;
  81.             ok=TRUE;
  82.         }
  83.         if (Args[ARG_ADDSTACK])
  84.         {
  85.             StackAdd=(*Args[ARG_ADDSTACK]+3)&0xFFFFFFFC;
  86.             ok=TRUE;
  87.         }
  88.         FreeArgs(MyArgs);
  89.     }
  90.     if (!ok) Cleanup(RETURN_WARN);
  91. }
  92.  
  93. /* NEW_CreateNewProc and NEW_SystemTagList initially
  94.    were written in C but have been converted to Assembly
  95.    language, later. So the routines here are commented
  96.    out!!
  97.    
  98.    Note: The ASM-Routines now call CloneTagItems
  99.    ===== The C-Routines here don't do that and may vary also
  100.          in some other ways!!!!
  101.  
  102. LONG NEW_CreateNewProc(struct TagItem *tags)
  103. {
  104.     struct TagItem newti[2] = {NP_StackSize,MinStack+StackAdd,TAG_MORE,(LONG)tags};
  105.  
  106.     struct TagItem *ti;
  107.  
  108.     if (tags && ((ti=FindTagItem(NP_StackSize,tags))))
  109.     {
  110.         if (ti->ti_Data<MinStack) ti->ti_Data=MinStack;
  111.         ti->ti_Data+=StackAdd;
  112.         return JumpOld_CreateNewProc(tags);
  113.     } else {
  114.         if (!tags) newti[1].ti_Tag=TAG_DONE;
  115.         return JumpOld_CreateNewProc(newti);
  116.     }
  117. }
  118.  
  119. LONG NEW_SystemTagList(STRPTR command, struct TagItem *tags)
  120. {
  121.     struct TagItem newti[2] = {NP_StackSize,MinStack+StackAdd,TAG_MORE,(LONG)tags};
  122.  
  123.     struct TagItem *ti;
  124.  
  125.     if (tags && ((ti=FindTagItem(NP_StackSize,tags))))
  126.     {
  127.         if (ti->ti_Data<MinStack) ti->ti_Data=MinStack;
  128.         ti->ti_Data+=StackAdd;
  129.         return JumpOld_SystemTagList(command,tags);
  130.     } else {
  131.         if (!tags) newti[1].ti_Tag=TAG_DONE;
  132.         return JumpOld_SystemTagList(command,newti);
  133.     }
  134.     
  135. }
  136. */
  137. void DoPatches(void)
  138. {
  139.     #define REALPOS(routine) (APTR) (((UBYTE *)routine) - ((UBYTE *)PATCHSTART) + mem)
  140.  
  141.     UBYTE *mem;
  142.     ULONG len=((UBYTE *)PATCHEND) - ((UBYTE *)PATCHSTART) + 1;
  143.  
  144.     if (!(mem=AllocMem(len,MEMF_PUBLIC))) Cleanup(RETURN_WARN);
  145.  
  146.     /* Now we know that StackAttack will not fail to run,
  147.        therefore Cleanup must not close utility.library
  148.        (because of the patches needing it)!! */
  149.        
  150.     DontCloseUtility=TRUE;
  151.  
  152.     Forbid();
  153.  
  154.     Old_CreateProc=SetFunction((struct Library *)DOSBase,LVO_CreateProc,REALPOS(New_CreateProc));
  155.     Old_CreateNewProc=SetFunction((struct Library *)DOSBase,LVO_CreateNewProc,REALPOS(New_CreateNewProc));
  156.     Old_SystemTagList=SetFunction((struct Library *)DOSBase,LVO_SystemTagList,REALPOS(New_SystemTagList));
  157.     Old_RunCommand=SetFunction((struct Library *)DOSBase,LVO_RunCommand,REALPOS(New_RunCommand));
  158.  
  159.     // we copymem only here because the Old_??
  160.     // variables will be copied, too!!
  161.  
  162.     CopyMem(PATCHSTART,mem,len);
  163.     CacheClearU(); // !!
  164.  
  165.     Permit();
  166. }
  167.  
  168. void main(void)
  169. {
  170.     Init();
  171.     OpenLibs();
  172.     GetArguments();
  173.     DoPatches();
  174.     Cleanup(RETURN_OK);
  175. }
  176.  
  177.     
  178.